package com.example.park2212.house

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.GridLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.entity.MyHourseBean
import com.example.lib_base.mvvm.BaseActivity
import com.example.park2212.R
import com.example.park2212.databinding.ActivityHoursBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/house/HoursActivity")
class HoursActivity : BaseActivity<ActivityHoursBinding,HoursViewModel>() {
   val hourseAreaAdapter by lazy { HourseAreaAdapter(viewModel) }

    override fun initView() {

        binding.rvHouse.apply {
            adapter = hourseAreaAdapter
            layoutManager = LinearLayoutManager(this@HoursActivity)
        }

        binding.parkaddRg.setOnCheckedChangeListener { radioGroup, i ->
            if (i == R.id.rb_a){
                viewModel.getHourseById(1)
            }else if (i == R.id.rb_b){
                viewModel.getHourseById(2)
            }else if (i == R.id.rb_c){
                viewModel.getHourseById(3)
            }else if (i == R.id.rb_d){
                viewModel.getHourseById(4)
            }
        }

    }

    override fun initData() {
        viewModel.houseSuccess.observe(this){
            hourseAreaAdapter.data.clear()
            //根据面积分组
            val map = it.groupBy { it.area }
            //根据面积排序
            val list = map.keys.sorted()

            val hourseBeans = mutableListOf<MyHourseBean>()
            var index = 1
            for (key in list){
                hourseBeans.add(MyHourseBean(index,key,map[key]!!))
                index++
            }
            hourseAreaAdapter.addData(hourseBeans)
            hourseAreaAdapter.notifyDataSetChanged()
        }
        viewModel.rentHouseSuccess.observe(this){
            ToastUtils.showLong("操作成功")
        }



    }

    override fun onResume() {
        super.onResume()
        viewModel.house()
        viewModel.getHourseById(1)
    }

}